package cn.slipi.admin.common.form;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

/**
 * 分页查询FORM
 *
 * @author lee
 */
@Data
public class PageQueryForm extends BaseForm {

    /**
     * 排序字段
     */
    private String orderBy;

    /**
     * 排序方向(asc,desc)
     */
    private String order;

    public static final String ASC = "asc";
    public static final String DESC = "desc";

    public PageRequest toPageRequest() {
        if (hasSortColumns()) {
            return PageRequest.of(getPageNo() - 1, getPageSize(), Sort.Direction.fromString(order), orderBy);
        } else {
            return PageRequest.of(getPageNo() - 1, getPageSize());
        }
    }

    public boolean hasSortColumns() {
        return StringUtils.isNotBlank(order) && StringUtils.isNotBlank(orderBy);
    }
}
